package com.yxwl.demo.ui.scan;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.print.base.bean.DeviceItem;
import com.print.base.listen.ScanListener;
import com.print.myprinter.ScannerBase;
import com.print.printer.PrinterManage;
import com.yxwl.demo.R;
import com.yxwl.demo.ui.base.BaseActivity;
import com.yxwl.demo.ui.print.PrintActivity;
import com.yxwl.demo.ui.adapter.BaseRecyclerViewAdapter;
import com.yxwl.demo.utils.OnCallback;
import com.yxwl.demo.utils.OnItemClickListner;
import com.yxwl.demo.ui.print.printer.PrinterUtil;
import com.yxwl.demo.widget.CustomProgress;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseScan extends BaseActivity {

    protected BaseRecyclerViewAdapter adapter;
    protected TextView titleTv;
    protected int model;
    public List<DeviceItem> list = new ArrayList<>();
    protected ScannerBase scanner;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scan_list;
    }

    @Override
    protected void initView() {
        titleTv = findViewById(R.id.title);
        titleTv.setText("SPP 列表");
        findViewById(R.id.back).setOnClickListener(getOnClickListen());
        findViewById(R.id.refresh).setOnClickListener(getOnClickListen());

        initAdapter();
        RecyclerView recyclerView = findViewById(R.id.list);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListner(new OnItemClickListner() {
            @Override
            public boolean onItemClickListner(View v, int type, int position, Object o) {
                if (o!=null){
                    PrinterUtil.getInstance().setDevice((DeviceItem) o, new OnCallback() {
                        @Override
                        public void onCallback(Object o) {
                            startAct(PrintActivity.class, null);
                            scanner.stopScan();
                        }
                    });
                }
                return false;
            }
        });

    }


    @Override
    protected void initData() {
        scanner = PrinterManage.getInstance().getScanner(model);
        scanner.setListener(new ScanListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onFound(DeviceItem item) {
                addDevice(item);
            }

            @Override
            public void onFinished() {

            }

            @Override
            public void onFailed(String msg) {

            }
        });
    }

    @Override
    protected void widgetClick(View view, int id) {
        if (id == R.id.back) {
            finish();
        } else if (id == R.id.refresh) {
            adapter.setDatas(null);
            list.clear();
            scan();
        }
    }

    protected abstract void initAdapter();

    protected void scan() {
        if (scanner!=null){
            scanner.scan();
        }
    }

    @Override
    protected void onStop() {
        if (scanner!=null){
            scanner.stopScan();
        }
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        if (scanner!=null){
            scanner.release();
        }
        super.onDestroy();
    }

    @SuppressLint("MissingPermission")
    protected void addDevice(DeviceItem item) {
        if (item == null) {
            return;
        }
        if (check(item.name)) {
            return;
        }
        for (DeviceItem data : list) {
            if (data.address.equals(item.address)) {
                return;
            }
        }
        CustomProgress.dismissPD();
        list.add(item);
        adapter.addData(item);
    }

    protected boolean check(String deviceName) {
        return deviceName.endsWith("_LE")
                || deviceName.endsWith("-LE")
                || deviceName.endsWith("_BLE")
                || deviceName.endsWith("-BLE")
                || deviceName.endsWith("phone")
                || deviceName.startsWith("RW401AP")
                || deviceName.startsWith("ITPP941AP")
                || deviceName.startsWith("Y486AP")
                || deviceName.startsWith("P44S");
    }

}
